//页面初始化
function init(){
           // alert();
			document.addEventListener("deviceready", ready, true);
            //alert();
           
} 
function ready(){
	var theme = localStorage.theme;
    changeTheme(theme);
	var rssChannel = localStorage.channel;
	var jsonObj = eval(rssChannel);
	var length = jsonObj.length;
   // alert(length);
	var html ="";
	var appendhtml="";
	for ( var i = 0; i < length; i++) {
		 if(jsonObj[i].enable){
		  var temp = jsonObj[i].url.replace(/http\:\/\//, "").replace(/\./g,"___").replace(/\//g, "---"); 
		  var key = "catch_"+temp;
          var rsscatch = localStorage[key];
                                           // alert(rsscatch.toString());
          if(rsscatch){                 
                 var news = eval(rsscatch);
                                            //alert(news.length);
				 for ( var j = 0; j < news.length; j++) {
				 	
					
					var linkid = news[j].link.replace(/http\:\/\//, "").replace(/\./g,"___").replace(/\//g, "---");
                    if(localStorage[linkid]=="1"){
                    	html+="<div style='background-color:green;color:white;border:1px solid black;margin:2px 0px;padding:4px' id='"+linkid+"' href='' onclick='javascript:go(&#39;"+news[j].link+"&#39;);' data-role='button' data-transition='slide'>"+news[j].title+"（点击查看）</div>"; 
					
                    }else{
                    	appendhtml+="<div  style='border:1px solid black;margin:2px 0px;padding:4px' id='"+linkid+"' href='' onclick='javascript:download(&#39;"+news[j].link+"&#39;);' >"+news[j].title+"(下载）</div>";                              
                    }
				 }

				 
		  }
		 }
	}
	 $("#off_list").html(html+appendhtml);
	 
}
function download(url){
	var linkid = url.replace(/http\:\/\//, "").replace(/\./g,"___").replace(/\//g, "---");
    localStorage.setItem("downloadUrl",url);
    var src = url.replace(/http\:\/\//, "").replace(/\//g, "%2F");
    localStorage.setItem("filename",src);
    $.mobile.showPageLoadingMsg("c","正在下载...",true);
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
   
    $("#"+linkid).attr("onclick","javascript:go('"+url+"');");
    
    localStorage.setItem(linkid,"1");
}
function fail(evt) {
     console.log(evt.target.error.code);
}
function onFileSystemSuccess(fileSystem) {
    //alert(fileSystem.name);
    //alert(fileSystem.root.name);
   /* var uri =  fileSystem.root.toURL().toString();
   uri= uri.replace(/file\:\/\/localhost/,"");
    alert(uri);*/
	var fileTransfer = new FileTransfer();
	//var uri = localStorage.downloadUrl;
    var baiduzm = "http://gate.baidu.com/tc?bd_page_type=4&src=";
    var src = localStorage.downloadUrl.replace(/http\:\/\//, "").replace(/\//g,"%2F");
     var uri = baiduzm+src;
     var filePath =  fileSystem.root.toURL().toString();
     filePath = filePath.replace(/file\:\/\//,"");
     filePath+="/";
     filePath+= localStorage.filename;
     var filename =  localStorage.filename;
        fileSystem.root.getFile(filename, null,
        function(fileEntry){
        },
		function(){
		        	
		        	window.plugins.fileDownloadAndroid07API.download(
				     
				     function(entry) {
		                                 //alert();
		                  $.mobile.hidePageLoadingMsg();
		                  var linkid = localStorage.downloadUrl.replace(/http\:\/\//, "").replace(/\./g,"___").replace(/\//g, "---");
		                  $("#"+linkid).attr("style","background-color:green;color:white");              
		             },
				     function(error) {
					  alert("网络异常： " + error.source);
					  
					 // alert("download error target " + error.target);
					  //alert("upload error code" + JSON.stringify(error));
				     },uri, filePath
		          );                                                             
		   });
    
	
}
function go(url) {
	var baiduzm = "http://gate.baidu.com/tc?bd_page_type=4&src=";
	var src = url.replace(/http\:\/\//, "").replace(/\//g, "%2F");
	// 设备的网络
	var networkState = navigator.network.connection.type;
	// window.location.href = url;
	// 如果设备使用的2g网络
	
		// 将新闻地址存储到本地html
		localStorage.url = baiduzm + src;
		localStorage.filename = src;
		$.mobile.showPageLoadingMsg("c", "Loading...", true);
		window.location.href = "info.html";
	
}                                                    
